{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #符文储备"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: runeReserve"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #符文储备"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "远征队在出发前需要携带一些「符文」，作为后续的冒险储备。`runes[i]` 表示第 `i` 枚符文的魔力值。\n",
    "\n",
    "他们将从中选取若干符文进行携带，并对这些符文进行重新排列，以确保任意相邻的两块符文之间的魔力值相差不超过 `1`。\n",
    "\n",
    "请返回他们能够携带的符文 **最大数量**。\n",
    "\n",
    "**示例 1：**\n",
    ">输入：`runes = [1,3,5,4,1,7]`\n",
    ">\n",
    ">输出：`3`\n",
    ">\n",
    ">解释：最佳的选择方案为[3,5,4]\n",
    ">将其排列为 [3,4,5] 后，任意相邻的两块符文魔力值均不超过 `1`，携带数量为 `3`\n",
    ">其他满足条件的方案为 [1,1] 和 [7]，数量均小于 3。\n",
    ">因此返回可携带的最大数量 `3`。\n",
    "\n",
    "**示例 2：**\n",
    ">输入：`runes = [1,1,3,3,2,4]`\n",
    ">\n",
    ">输出：`6`\n",
    ">\n",
    ">解释：排列为 [1,1,2,3,3,4]，可携带所有的符文\n",
    "\n",
    "**提示：**\n",
    "- `1 <= runes.length <= 10^4`\n",
    "- `0 <= runes[i] <= 10^4`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [W2ZX4X](https://leetcode.cn/problems/W2ZX4X/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [W2ZX4X](https://leetcode.cn/problems/W2ZX4X/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,3,5,4,1,7]', '[1,1,3,3,2,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for i in range(1, len(runes)):\n",
    "            if runes[i] - runes[i - 1] <= 1:\n",
    "                cnt+=1\n",
    "            else:\n",
    "                cnt = 1\n",
    "            ans = max(ans, cnt)\n",
    "        return ans\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        res=0\n",
    "        count=0\n",
    "        for i in range(0,len(runes)):\n",
    "            if runes[i]-runes[i-1]<=1:\n",
    "                count+=1\n",
    "            else:\n",
    "                res=max(res,count)\n",
    "                count=1\n",
    "        return max(res,count)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        max_len = 0\n",
    "        temp = 1\n",
    "        if len(runes) == temp:\n",
    "            return(temp)\n",
    "        for i in range(len(runes)-1):\n",
    "            if runes[i+1] - runes[i] > 1:\n",
    "                temp = 1\n",
    "            else:\n",
    "                temp += 1\n",
    "            max_len = max(temp, max_len)\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        res,tmp=1,1\n",
    "        for i in range(1,len(runes)):\n",
    "            if runes[i]-runes[i-1]<=1:\n",
    "                tmp+=1\n",
    "            else:\n",
    "                res=max(res,tmp)\n",
    "                tmp=1\n",
    "        res=max(res,tmp)\n",
    "        return res    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes = sorted(runes)\n",
    "        n = len(runes)\n",
    "        r = 1\n",
    "        t = 1\n",
    "        for i in range(n - 1):\n",
    "            if runes[i] < runes[i + 1] - 1:\n",
    "                t = 1\n",
    "            else:\n",
    "                t += 1\n",
    "            r = max(t, r)\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        n = ans = 0\n",
    "        for i in range(len(runes)-1):\n",
    "            if 1 >= runes[i+1] - runes[i] >= 0:\n",
    "                n += 1\n",
    "                ans = max(ans, n)\n",
    "            else:\n",
    "                n = 0\n",
    "        return ans + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes = sorted(runes)\n",
    "        m = 1\n",
    "        tmp = 1\n",
    "        for i in range(1,len(runes)):\n",
    "            if abs(runes[i]-runes[i-1])>=2:\n",
    "                if m<tmp:\n",
    "                    m=tmp\n",
    "                tmp = 1\n",
    "            else: \n",
    "                tmp += 1\n",
    "        if m<tmp:\n",
    "            m = tmp\n",
    "        \n",
    "        return m\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        max_len = 0\n",
    "        temp = 1\n",
    "        if len(runes) == temp:\n",
    "            return(temp)\n",
    "        for i in range(len(runes)-1):\n",
    "            if runes[i+1] - runes[i] <= 1:\n",
    "                temp += 1\n",
    "                if i == len(runes)-2:\n",
    "                    max_len = max(temp, max_len)\n",
    "                continue\n",
    "            max_len = max(temp, max_len)\n",
    "            temp = 1\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1  # 重新统计\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans,cnt)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1  # 重新统计\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        lis = []\n",
    "        i = 0\n",
    "        tmp = 1\n",
    "        while i < len(runes):\n",
    "            while i < len(runes) - 1 and runes[i + 1] - runes[i] < 2:\n",
    "                tmp += 1\n",
    "                i += 1\n",
    "            lis.append(tmp)\n",
    "            tmp = 1\n",
    "            i += 1\n",
    "        return max(lis)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans,cnt)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        i = 0\n",
    "        n = len(runes)\n",
    "        result = 0\n",
    "        while i < n:\n",
    "            cur_num = 1\n",
    "            j = i+1\n",
    "            while j < n and runes[j] - runes[j-1] <= 1:\n",
    "                cur_num += 1\n",
    "                j += 1\n",
    "            result = max(cur_num,result)\n",
    "            i = j\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        dp = 1\n",
    "        return max((dp := (y <= x + 1) * dp + 1 for x, y in pairwise(sorted(runes))), default=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        j = runes.append(float('inf')) or 0\n",
    "        return max(-j + (j := i) for i in range(1, len(runes)) if runes[i] > runes[i-1] + 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        if len(runes) == 1:\n",
    "            return 1\n",
    "        runes.sort()\n",
    "        ans = 0\n",
    "        cnt = 1\n",
    "        for i in range(1, len(runes)):\n",
    "            if runes[i] - runes[i - 1] <= 1:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "            else:\n",
    "                cnt = 1\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        print(runes)\n",
    "        ans = cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        prev = runes[0]\n",
    "        cnt = 0\n",
    "        m = 0\n",
    "        for r in runes:\n",
    "            if r - prev <= 1:\n",
    "                cnt += 1\n",
    "                prev=r\n",
    "                m = max(m, cnt)\n",
    "            else:\n",
    "                prev = r\n",
    "                cnt = 1\n",
    "        return m"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        ans = -1\n",
    "        runes.sort()\n",
    "        las = -10\n",
    "        t = 0\n",
    "        for i in runes:\n",
    "            if abs(i-las)<=1:\n",
    "                t+=1\n",
    "            else:\n",
    "                ans = max(ans,t)\n",
    "                t = 1\n",
    "            las = i\n",
    "        ans = max(ans,t)\n",
    "        # print(runes)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = 1\n",
    "        cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        res = 0\n",
    "        right = max(runes)\n",
    "        cnt = defaultdict(int)\n",
    "        for run in runes:\n",
    "            cnt[run] += 1\n",
    "        while right >= 0:\n",
    "            x = 0\n",
    "            while cnt[right] > 0:\n",
    "                x += cnt[right]\n",
    "                right -= 1\n",
    "            res = max(res, x)\n",
    "            right -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        res = 0\n",
    "        tmp = 1\n",
    "        for i in range(1, len(runes)):\n",
    "            if runes[i] - runes[i - 1] <= 1:\n",
    "                tmp += 1\n",
    "            else:\n",
    "                res = max(res, tmp)\n",
    "                tmp = 1\n",
    "        res = max(res, tmp)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        c=1\n",
    "        b=1\n",
    "        for i in range(len(runes)-1):\n",
    "            if abs(runes[i]-runes[i+1])<=1:\n",
    "                c+=1\n",
    "                b=max(b,c)\n",
    "            else:\n",
    "                c=1\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        runes_len = len(runes)\n",
    "        count = 1\n",
    "        count_max = 0\n",
    "        if runes_len == 1:\n",
    "            return 1\n",
    "        for i in range(0, runes_len - 1):\n",
    "            if runes[i + 1] - runes[i] <= 1:\n",
    "                count += 1\n",
    "                count_max = max(count_max, count)\n",
    "\n",
    "            else:\n",
    "                count = 1\n",
    "\n",
    "        return count_max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        if len(runes) == 1:\n",
    "            return 1\n",
    "        runes = sorted(runes)\n",
    "        m, n = 0, 0\n",
    "        for i in range(0, len(runes) - 1):\n",
    "            if runes[i + 1] - runes[i] <= 1:\n",
    "                m += 1\n",
    "\n",
    "            elif runes[i + 1] - runes[i] > 1:\n",
    "                n = max(n, m)\n",
    "                m = 0\n",
    "        n = max(n, m)+1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        \n",
    "        lenth=1\n",
    "        maxx=1\n",
    "\n",
    "        for i in range(len(runes)-1):\n",
    "            if runes[i+1]-runes[i]<=1:\n",
    "                lenth+=1\n",
    "            if runes[i+1]-runes[i]>1 or i==len(runes)-2:\n",
    "                maxx=max(maxx,lenth)\n",
    "                lenth=1\n",
    "\n",
    "        return maxx\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        # runes.sort()\n",
    "        # ans = cnt = 1\n",
    "        # for pre, cur in pairwise(runes):\n",
    "        #     if cur - pre > 1:\n",
    "        #         cnt = 1\n",
    "        #     else:\n",
    "        #         cnt += 1\n",
    "        #         ans = max(ans, cnt)\n",
    "        # return ans\n",
    "        ans = 1\n",
    "        runes.sort()\n",
    "        stack = [runes[0]]\n",
    "        for i in range(1, len(runes)):\n",
    "            if runes[i] - stack[-1] > 1:\n",
    "                ans = max(ans, len(stack))\n",
    "                stack = [runes[i]]\n",
    "            else:\n",
    "                stack.append(runes[i])\n",
    "        return max(ans, len(stack))\n",
    "\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1  # 重新统计\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1  # 重新统计\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans, count = 1, 1\n",
    "\n",
    "        for i in range(1, len(runes)):\n",
    "            if runes[i] - runes[i-1] > 1:\n",
    "                count = 1\n",
    "            else:\n",
    "                count += 1\n",
    "                ans = max(ans, count)\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        if len(runes) == 1:\n",
    "            return 1\n",
    "        runes = sorted(runes)\n",
    "        m, n = 0, 0\n",
    "        for i in range(0, len(runes) - 1):\n",
    "            if runes[i + 1] - runes[i] <= 1:\n",
    "                m += 1\n",
    "\n",
    "            elif runes[i + 1] - runes[i] > 1:\n",
    "                n = max(n, m)\n",
    "                m = 0\n",
    "        n = max(n, m)+1\n",
    "        return n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        l=len(runes)\n",
    "        i=count=result=1\n",
    "        while(i<l):\n",
    "            if runes[i]-runes[i-1]<=1:\n",
    "                count+=1\n",
    "            else:\n",
    "                if count>result:\n",
    "                    result=count\n",
    "                count=1\n",
    "            i+=1\n",
    "        if count>result:\n",
    "            result=count\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        if(len(runes)<=1):\n",
    "            return len(runes)\n",
    "        runes.sort()\n",
    "\n",
    "        ans=1\n",
    "        a=1\n",
    "        for i in range(1,len(runes)):\n",
    "            if(abs(runes[i]-runes[i-1])<=1):\n",
    "                a=a+1\n",
    "            else:\n",
    "                if(a>ans):\n",
    "                    ans=a\n",
    "                a=1\n",
    "\n",
    "        return max(a,ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        i=0\n",
    "        n=len(runes)\n",
    "        tmp=1\n",
    "        res=[]\n",
    "        while i <n:\n",
    "            while i <n-1 and runes[i+1]-runes[i]<2:\n",
    "                tmp+=1\n",
    "                i+=1\n",
    "            res.append(tmp)\n",
    "            tmp=1\n",
    "            i+=1\n",
    "        return max(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        left = 0\n",
    "        cur = runes[0]\n",
    "        leng = 0\n",
    "        for right, i in enumerate(runes):\n",
    "            if 1 >= i - cur >= 0:\n",
    "                leng = max(leng, right - left + 1)\n",
    "            else:\n",
    "                leng = max(leng, right - left)\n",
    "                left = right\n",
    "            cur = i\n",
    "        return leng"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        return max((len(m.group()) + 1 for m in re.finditer(b'\\x01+', bytes(b - a < 2 for a, b in pairwise(runes)))), default=1)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        lis=[]\n",
    "        i=0\n",
    "        tmp=1\n",
    "        while i<len(runes):\n",
    "            while i<len(runes)-1 and runes[i+1]-runes[i]<2:\n",
    "                tmp+=1\n",
    "                i+=1\n",
    "            lis.append(tmp)\n",
    "            tmp=1\n",
    "            i+=1\n",
    "        return max(lis)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = 1\n",
    "        temp = 1\n",
    "        for i in range(1, len(runes)):\n",
    "            if abs(runes[i] - runes[i-1]) <= 1:\n",
    "                temp += 1\n",
    "            else: \n",
    "                ans = max(ans, temp)\n",
    "                temp = 1\n",
    "        ans = max(ans, temp)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        cnt, left, right = 1, 0, 0\n",
    "        for right in range(1,len(runes)):\n",
    "            if runes[right]-runes[right-1]>1:\n",
    "                cnt=max(cnt,right-left)\n",
    "                left=right\n",
    "        return max(cnt,right-left+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        res = ans = 1\n",
    "        for (i, j) in enumerate(runes[:-1]):\n",
    "            if abs(j - runes[i + 1]) <= 1:\n",
    "                ans += 1\n",
    "            else:\n",
    "                res = max(res, ans)\n",
    "                ans = 1\n",
    "        return max(res, ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for i in range(1, len(runes)):\n",
    "            pre, cur = runes[i-1], runes[i]\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1  # 重新统计\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = 1\n",
    "        t = 1\n",
    "        for i in range(1, len(runes)):\n",
    "            if runes[i] <= runes[i - 1] + 1:\n",
    "                t += 1\n",
    "            else:\n",
    "                ans = max(ans, t)\n",
    "                t = 1\n",
    "        return max(ans, t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def runeReserve(self, runes: List[int]) -> int:\n",
    "        runes.sort()\n",
    "        ans = cnt = 1\n",
    "        for pre, cur in pairwise(runes):\n",
    "            if cur - pre > 1:\n",
    "                cnt = 1  # 重新统计\n",
    "            else:\n",
    "                cnt += 1\n",
    "                ans = max(ans, cnt)\n",
    "        return ans\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
